{% extends "base.html" %}
{% block title %}检查列表{% endblock %}
{% block head %}
    <meta HTTP-EQUIV="pragma" CONTENT="no-cache">
    <meta HTTP-EQUIV="Cache-Control" CONTENT="no-store, must-revalidate">
    <meta HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
    <meta HTTP-EQUIV="expires" CONTENT="0">
    {{ super() }}
    <style>
        .weui-cells__title {
            padding-left: 0;
        }

        .weui-well__top, .weui-well__content {
            border: none;
        }

        #onkeyBDBtn {
            position: fixed;
            bottom: 10px;
            left: 0px;
            right: 0px;
        }

        .weui-dropdown {
            border: none;
            z-index: 3;
        }

        .drop-down-mask {
            background: #000000;
            opacity: 0.6;
            width: 100%;
            height: 100%;
            position: absolute;
            z-index: 2;
        }

        .weui-btn_common {
            background: #ffffff;
            color: #4697F3;
            font-size: 16px;
        }

        .weui_button.selected {
            background: #4697F3;
            color: #fff;
        }

    </style>
{% endblock %}
{% block body %}
    {#    {% if not showTitle %}#}
    {#        {% import "component/header.html" as header %}#}
    {#        {{ header.header(true,"检查列表","") }}#}
    {#    {% endif %}#}
    <div class="contianer">
    <div class="mine-page" style="overflow-y: hidden">
        <div class="weui-flex" id="clinic_type_btnGroup">
            <div class="weui-flex__item">
                <div class="btn-group">
                    <a href="javascript:void(0)" data-value="1" class="selected">待报到</a>
                    <a href="javascript:void(0)" data-value="2" class="">已报到</a>
                </div>
            </div>
            <div id="filterBtn" class="text-common" style="margin-right: 5px;">
                <i class='iconfont'>&#xe686;</i><span class="text-blank" style="width: 1em;">筛选</span>
            </div>
        </div>
        <div class="ccdList-page" style="overflow-x: hidden;top: 4em;">
            <div class="weui-pull-to-refresh__layer" style="padding:7px;">
                <div class='weui-pull-to-refresh__arrow'></div>
                <div class='weui-pull-to-refresh__preloader'></div>
                <div class="down">下拉刷新</div>
                <div class="up">释放刷新</div>
                <div class="refresh">正在刷新</div>
            </div>

            <div id="ccd-list" style="margin-bottom:5em;">
            </div>
            <div class="weui-infinite-scroll" style="display: none;">
                <div class="infinite-preloader"></div>
                <div class="weui-loadmore" style="margin: 0 auto">
                    <i class="weui-loading"></i>
                    <span class="weui-loadmore__tips">正在加载</span>
                </div>
            </div>
        </div>
        <div class="drop-down-mask" style="display: none"></div>
        <div id="type-drop-down" class="weui-dropdown">
            <div class="">
                <div class="text-disabled" style="padding:5px 15px;">检查类型</div>
                <div id="type-group" style="padding:10px">
                    <div class="weui_button" data-value="1">全部</div>
                    <div class="weui_button selected" data-value="2">内镜</div>
                    <div class="weui_button" data-value="3">放射1</div>
                    <div class="weui_button" data-value="4">放射2</div>
                    <div class="weui_button" data-value="5">放射3</div>
                    <div class="weui_button" data-value="6">超声</div>
                    <div class="weui_button" data-value="7">核医院</div>
                </div>
                <div class="weui-flex">
                    <div class="weui-flex__item" style="padding: 15px;">
                        <div id="filterResetBtn" class="weui-btn weui-btn_common">重置</div>
                    </div>
                    <div class="weui-flex__item" style="padding: 15px;">
                        <div id="filterOkBtn" class="weui-btn weui-btn_primary">确定</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block script %}
    {{ super() }}
    <script type="text/javascript">

        var cacheDataList = null;
        var select_type = "2";
        var select_state = 1;
        var batchRes = [];//批量预约的预约号
        var user_id = common.getQueryString("user_id");
        $("#clinic_type_btnGroup a").click(function () {
            $(this).parent().find(".selected").removeClass("selected");
            $(this).addClass("selected");
            select_state = $(this).attr("data-value");
            $("#onkeyBDBtn").remove();
            $(".drop-down-mask").hide();
            $("#type-drop-down").hide();
            getCCDList();
        });


        $("#filterBtn").click(function () {
            var isShow = $("#type-drop-down").css("display") === 'block';
            if (isShow) {
                $(".drop-down-mask").hide();
            } else {
                $(".drop-down-mask").show();
            }
            $("#type-drop-down").slideToggle("fast");
        });
        $("#type-group>.weui_button").click(function () {
            if ($(this).attr("data-value") == "1") {
                $(this).parent().find("div.weui_button.selected").removeClass("selected");
            } else {
                $(this).parent().find(".weui_button[data-value=1]").removeClass("selected");
            }
            $(this).toggleClass("selected");
        });

        $("#filterResetBtn").click(function () {
            $("#type-group").find("div.weui_button.selected").removeClass("selected");
            $("#type-group").find(".weui_button[data-value=1]").addClass("selected");
        });
        $("#filterOkBtn").click(function () {
            var typeList = [];
            var selectTypeBtn = $("#type-group").find("div.weui_button.selected");
            for (var i = 0; i < selectTypeBtn.length; i++) {
                typeList.push(selectTypeBtn.eq(i).attr("data-value"));
            }
            console.log(typeList);
            select_type = typeList.join(",");
            $(".drop-down-mask").hide();
            $("#type-drop-down").hide();
            getCCDList();
        });
        $(function () {
            if (sessionStorage.getItem("list_type") != null) {
                select_type = parseInt(sessionStorage.getItem("list_type"));
                sessionStorage.removeItem("list_type");
            }
            if (sessionStorage.getItem("list_state") != null) {
                select_state = parseInt(sessionStorage.getItem("list_state"));
                $("#clinic_type_btnGroup a.selected").removeClass("selected");
                $("#clinic_type_btnGroup a[data-value='" + select_state + "']").addClass("selected");
                sessionStorage.removeItem("list_state");
            }
            getCCDList();
            $(".ccdList-page").pullToRefresh({
                onRefresh: function () { /* 当下拉刷新触发的时候执行的回调 */
                    $("#ccd-list").empty();
                    getCCDList("");
                    $(".ccdList-page").pullToRefreshDone();
                },
                onPull: function (percent) { /* 用户下拉过程中会触发，接收一个百分比表示用户下拉的比例 */
                },
                distance: 50 /* 下拉刷新的触发距离， 注意，如果你重新定义了这个值，那么你需要重载一部分CSS才可以，请参考下面的自定义样式部分 */
            });

        })

        function getCCDList() {
            $("#ccd-list").empty();
            $("#onkeyBDBtn").remove();
            batchRes = [];
            var types = [];
            $.showLoading();

            var params = {
                "patient_id": common.getQueryString("user_id"),
                "select_type": select_type,
                "select_state": select_state
            };
            {#var start_time = common.getNextDay(select_time);#}
            {#var end_time = common.getNow();#}
            $.post(navRoot + "/api/baodao/list", params, function (data) {
                if (data["code"] == 200) {
                    var list = [];
                    try {
                        if (data["data"]["List"]) {

                            var dataObj = data["data"]["List"]["RequisitionFindResult"]["MedicalRequestInfo"]["RequestInfo"];
                            var patientInfo = data["data"]["List"]["RequisitionFindResult"]["PatientInfo"];
                            if (!dataObj.hasOwnProperty("length")) {
                                list[0] = dataObj;
                            } else {
                                list = dataObj;
                            }
                            cacheDataList = list;
                            for (var i in list) {
                                var requestInfo = list[i];
                                var exam_ScheduleID = requestInfo['ReservationInfo']["ScheduleID"];
                                if (!exam_ScheduleID || types.indexOf(exam_ScheduleID.replace("@", "-")) == -1) {
                                    if (exam_ScheduleID) {
                                        exam_ScheduleID = exam_ScheduleID.replace("@", "-");
                                        types.push(exam_ScheduleID);
                                    }
                                    document.title = patientInfo['PatientName'] + "，" + (patientInfo['PatientSex'] == "2" ? "女" : "男") + "，" + (patientInfo['PatientAge'] || "");
                                    var typeItem = $('' +
                                        '<div class="weui-well weui-well_access type' + exam_ScheduleID + '">' +
                                        '   <div class="weui-well__top weui-flex">' +
                                        '       <div class="font14 weui-flex__item inOrOutTime text-disabled">' +
                                        {#'<div class="font12 text-disabled">' + patientInfo['PatientName'] + "（" + (patientInfo['PatientSex'] == "2" ? "女" : "男") + "）" + patientInfo['PatientAge'] + "岁 用户ID：" + patientInfo['PatientID'] + '</div>' +#}
                                        '申请单号：' + requestInfo["SheetID"] + '</div>' +
                                        '       <div class="exam_state font12 font-bold text-common">' + '</div>' +
                                        '   </div>' +
                                        '   <div class="weui-well__content">' +
                                        '       <div class="weui-cell weui-cell_access">' +
                                        '           <div class="weui-cell__bd">' +
                                        '               <div class="weui-flex">' +
                                        '                   <div class="weui-flex__item font-bold examBodyPart"></div>' +
                                        '               </div>' +
                                        '               <div class="font14">' + requestInfo["DepartMent"] + '</div>' +
                                        '               <div class="font14">预约时间：<span class="text-red">' + common.dateFormart(requestInfo["ReservationInfo"]["resDate"], "yyyy-MM-dd") + " " + common.dateFormart(requestInfo["ReservationInfo"]["resTime"]) + '</div>' +
                                        '           </div>' +
                                        '           <div class="weui-cell__ft"/>' +
                                        '       </div>' +
                                        '       <div class="weui-cells__title" style="display:none">同一个预约的其他申请单</div>' +
                                        '           <div class="sheetList weui-cells">' +
                                        '       </div>' +
                                        '   </div>' +
                                        '   <div class="weui-well__footer optionBtn weui-flex">' +
                                        '       <div class="weui-flex__item"></div>' +
                                        '   </div>' +
                                        '</div>'
                                    );
                                    var examBodyParts = "";
                                    if (requestInfo["ExamList"]["Exam"].length) {
                                        for (var m = 0; m < requestInfo["ExamList"]["Exam"].length; m++) {
                                            if (m == 0) {
                                                examBodyParts = requestInfo["ExamList"]["Exam"][0]["ExamBodyPart"];
                                            } else {
                                                examBodyParts += "，" + requestInfo["ExamList"]["Exam"][m]["ExamBodyPart"];
                                            }
                                        }
                                    } else {
                                        examBodyParts = requestInfo["ExamList"]["Exam"]["ExamBodyPart"]
                                    }
                                    typeItem.find(".examBodyPart").text(examBodyParts);
                                    var baodaoBtn = $("<button class=\"baodaoBtn weui-btn weui-btn_mini\">报到</button>");
                                    var reservationInfo = $("<button class=\"reservationInfo weui-btn weui-btn_mini\">预约检查单</button>");
                                    var paiduiInfo = $("<button class=\"paiduiInfo weui-btn weui-btn_mini\">查看排队</button>");
                                    var baodaoInfo = $("<button class=\"baodaoInfo weui-btn weui-btn_mini\">报到回执单</button>");
                                    reservationInfo.data("reservationInfo", requestInfo);
                                    //进入预约回执单
                                    reservationInfo.click(function (event) {
                                        event.preventDefault();
                                        event.stopPropagation();
                                        sessionStorage.setItem("list_scrollTop", $(".ccdList-page").scrollTop());
                                        sessionStorage.setItem("list_type", select_type);
                                        sessionStorage.setItem("list_state", select_state);
                                        var requestInfo = $(this).data("reservationInfo");
                                        if (requestInfo["ReservationInfo"]["ScheduleID"]) {
                                            location.href = navRoot + "/reservation/info?scheduleID=" + requestInfo["ReservationInfo"]["ScheduleID"] + "&sheetID=" + requestInfo["SheetID"] + "&type=" + select_type;
                                        } else {
                                            $.alert("没有预约号，无法查看预约回执单");
                                        }

                                    });
                                    baodaoInfo.data("baodaoInfo", requestInfo);
                                    //进入报到回执单
                                    baodaoInfo.click(function (event) {
                                        event.preventDefault();
                                        event.stopPropagation();
                                        sessionStorage.setItem("list_scrollTop", $(".ccdList-page").scrollTop());
                                        sessionStorage.setItem("list_type", select_type);
                                        sessionStorage.setItem("list_state", select_state);
                                        var requestInfo = $(this).data("baodaoInfo");
                                        location.href = navRoot + "/baodao/info?scheduleID=" + (requestInfo["ReservationInfo"]["ScheduleID"] || "") + "&sheetID=" + requestInfo["SheetID"] + "&type=" + select_type;
                                    });
                                    //进入排队
                                    paiduiInfo.click(function (event) {
                                        event.preventDefault();
                                        event.stopPropagation();
                                        {#$.alert("暂未开放，敬请期待！");#}
                                        sessionStorage.setItem("list_scrollTop", $(".ccdList-page").scrollTop());
                                        sessionStorage.setItem("list_type", select_type);
                                        sessionStorage.setItem("list_state", select_state);
                                        var requestInfo = $(this).data("baodaoInfo");
                                        location.href = navRoot + "/baodao/queue?sheetID=" + requestInfo["SheetID"];

                                    });
                                    //如果是未报到
                                    if (select_state == 1) {
                                        typeItem.find(".weui-cell__bd").append($('<div class="font12 weui-flex text-disabled ">' +
                                            '       <div class="weui-flex__item">开单医生：' + requestInfo["ReqSheetDoctor"] + '</div>' +
                                            '       <div class="text-right">开单时间：' + common.dateFormart(requestInfo["ReqSheetTime"]) + '</div>' +
                                            '</div>'));
                                        if (!exam_ScheduleID) {
                                            baodaoBtn.addClass("weui-btn_disabled");
                                            reservationInfo.addClass("weui-btn_disabled");
                                        }
                                        typeItem.find(".optionBtn").append(baodaoBtn);
                                        typeItem.find(".optionBtn").append(reservationInfo)
                                    }
                                    //如果是已报到
                                    else if (select_state == 2) {
                                        typeItem.find(".weui-cell__bd").append($('<div class="font14 weui-flex">排队号码：<span class="text-red">' + (requestInfo["ReservationInfo"]["QueueNum"] || "") + "</span></div>"));
                                        typeItem.find(".optionBtn").append(paiduiInfo);
                                        typeItem.find(".optionBtn").append(baodaoInfo);
                                    }
                                    var resDate = common.dateFormart(requestInfo["ReservationInfo"]["resDate"], 'yyyy-MM-dd');
                                    var resTime = requestInfo["ReservationInfo"]["resTime"];
                                    if (resDate) {//有预约时间
                                        //判断是否在预约时间内，在的话可以进行报到，并加入一键预约列表
                                        if (resTime && resTime.split("-").length > 1 && common.isOutTime(resDate, resTime.split("-")[1])) {
                                            typeItem.find(".baodaoBtn").addClass("weui-btn_disabled");
                                            typeItem.find(".baodaoBtn").click(function (event) {
                                                event.preventDefault();
                                                event.stopPropagation();
                                                $.alert("报到时间已过，请重新预约");
                                            });
                                        } else if (resTime && resTime.split("-").length > 1 && common.isDaoTime(resDate, resTime.split("-")[0])) {
                                            typeItem.find(".baodaoBtn").addClass("weui-btn_disabled");
                                            typeItem.find(".baodaoBtn").click(function (event) {
                                                event.preventDefault();
                                                event.stopPropagation();
                                                $.alert("报到时间未到，请预约时间进行操作");
                                            });
                                        } else {
                                            var ScheduleID = requestInfo["ReservationInfo"]["ScheduleID"];
                                            if (ScheduleID && batchRes.indexOf(ScheduleID) == -1) {
                                                batchRes.push({
                                                    "sheetID": requestInfo["SheetID"],
                                                    "scheduleID": ScheduleID,
                                                    "type": select_type
                                                });
                                            }
                                            typeItem.find(".baodaoBtn").data("data", requestInfo);
                                            //点击报到按钮进行报到
                                            typeItem.find(".baodaoBtn").click(function (event) {
                                                event.preventDefault();
                                                event.stopPropagation();
                                                var requestInfo = $(this).data("data");
                                                var params = {
                                                    "sheetID": requestInfo["SheetID"],
                                                    "scheduleID": requestInfo["ReservationInfo"]["ScheduleID"],
                                                    "type": select_type
                                                };
                                                $.post(navRoot + "/api/inpatient/baodao", params, function (data) {
                                                    if (data["code"] == 200) {
                                                        var result = data["data"];
                                                        sessionStorage.setItem("list_scrollTop", $(".ccdList-page").scrollTop());
                                                        sessionStorage.setItem("list_type", select_type);
                                                        sessionStorage.setItem("list_state", select_state);
                                                        {#location.href = navRoot + "/baodao/info?scheduleID=" + (params["scheduleID"] || "") + "&sheetID=" + params["sheetID"] + "&type=" + params["type"] + "&queueName=" + result["QueueName"] + "&queueNumber=" + result["QueueNumber"];#}
                                                        location.href = navRoot + "/baodao/success?scheduleID=" + (params["scheduleID"] || "") + "&sheetID=" + params["sheetID"] + "&type=" + params["type"] + "&result=" + JSON.stringify(result);
                                                    } else {
                                                        {#$.alert(data["errmsg"]);#}
                                                        location.href = navRoot + "/baodao/fail?err=" + data["errmsg"];
                                                    }
                                                }, "json")
                                            })
                                        }
                                    } else {
                                        typeItem.find(".baodaoBtn").addClass("weui-btn_disabled");
                                        typeItem.find(".baodaoBtn").click(function (event) {
                                            event.preventDefault();
                                            event.stopPropagation();
                                            $.alert("无预约时间，无法报到");
                                        });
                                    }

                                    var examState = requestInfo["FeeState"];
                                    if (examState == "0") {
                                        typeItem.find(".exam_state").text("未收费");
                                    } else if (examState == "1") {
                                        typeItem.find(".exam_state").text("已收费");
                                    }
                                    typeItem.find(".weui-cells__title").click(function () {
                                        event.preventDefault();
                                        event.stopPropagation();
                                        $(this).parent().find(".sheetList").slideToggle('fast');
                                    });
                                    typeItem.data("info", requestInfo);
                                    typeItem.click(function () {
                                        var id = $(this).data("info")["SheetID"];
                                        var scheduleID = $(this).data("info")["ReservationInfo"]["ScheduleID"];
                                        var type = $(this).data("info")["RequisitionType"];
                                        sessionStorage.setItem("list_scrollTop", $(".ccdList-page").scrollTop());
                                        sessionStorage.setItem("list_type", select_type);
                                        sessionStorage.setItem("list_state", select_state);
                                        location.href = navRoot + "/info?sheetID=" + id + "&scheduleID=" + "&type=" + type;
                                    });
                                    $("#ccd-list").append(typeItem);
                                } else {
                                    var yyTypeItem = $("#ccd-list").find(".weui-well.type" + exam_ScheduleID.replace("@", "-"));
                                    var item = $(
                                        '<div class="weui-cell weui-cell_access" style="padding:5px;">' +
                                        '       <div class="weui-cell__bd">' +
                                        '           <div class="font14 weui-flex">' +
                                        '               <span class="weui-flex__item examBodyPart"></span>' +
                                        '排队号：' +
                                        '               <span class="text-red">' + (requestInfo["ReservationInfo"]["QueueNum"] || "") + '</span>' +
                                        '           </div>' +
                                        '           <div class="font12 text-disabled">预约时间：' + common.dateFormart(requestInfo["ReservationInfo"]["resDate"], "yyyy-MM-dd") + " " + common.dateFormart(requestInfo["ReservationInfo"]["resTime"]) + '</div>' +
                                        '       </div>' +
                                        '       <div class="weui-cell__ft"></div>' +
                                        '</div>');
                                    var examBodyParts = "";
                                    if (requestInfo["ExamList"]["Exam"].length) {
                                        for (var m = 0; m < requestInfo["ExamList"]["Exam"].length; m++) {
                                            if (m == 0) {
                                                examBodyParts = requestInfo["ExamList"]["Exam"][0]["ExamBodyPart"];
                                            } else {
                                                examBodyParts += "，" + requestInfo["ExamList"]["Exam"][m]["ExamBodyPart"];
                                            }
                                        }
                                    } else {
                                        examBodyParts = requestInfo["ExamList"]["Exam"]["ExamBodyPart"]
                                    }
                                    item.find(".examBodyPart").text(examBodyParts);
                                    item.data("info", requestInfo);
                                    item.click(function () {
                                        event.preventDefault();
                                        event.stopPropagation();
                                        var id = $(this).data("info")["SheetID"];
                                        var type = $(this).data("info")["RequisitionType"];
                                        sessionStorage.setItem("list_scrollTop", $(".ccdList-page").scrollTop());
                                        sessionStorage.setItem("list_type", select_type);
                                        sessionStorage.setItem("list_state", select_state);
                                        location.href = navRoot + "/info?sheetID=" + id + "&type=" + type;
                                    });
                                    yyTypeItem.find(".weui-cells__title").show();
                                    yyTypeItem.find(".weui-well__content>.sheetList").append(item);
                                }
                            }
                            //如果存在两个或者以上满足条件的预约或申请单的话，则可以进行批量报到
                            if (select_state == 1 && batchRes.length >= 1) {
                                var batItem = $('<div class="weui-loadmore" id="onkeyBDBtn" style="width:85%;"><div class="weui-btn weui-btn_primary">一键报到</div></div>');
                                batItem.click(function () {
                                    for (var r in batchRes) {
                                        var pars = batchRes[r];
                                        $.post(navRoot + "/api/inpatient/baodao", pars, function (data) {
                                            if (data["code"] == 200) {
                                                if (batchRes.indexOf(pars) == batchRes.length - 1) {
                                                    select_state = 2;
                                                    $("#clinic_type_btnGroup a.selected").removeClass("selected");
                                                    $("#clinic_type_btnGroup a[data-value='" + select_state + "']").addClass("selected");
                                                    $("#onkeyBDBtn").remove();
                                                    getCCDList();
                                                }
                                                console.log("报到成功");
                                            } else {
                                                $.alert(data["errmsg"]);
                                            }
                                        }, "json")
                                    }
                                    select_state = 2;
                                    $("#clinic_type_btnGroup a.selected").removeClass("selected");
                                    $("#clinic_type_btnGroup a[data-value='" + select_state + "']").addClass("selected");
                                    $("#onkeyBDBtn").remove();
                                    getCCDList();
                                })

                                $(".ccdList-page").append(batItem);
                            }
                            if ($("#ccd-list").children().length == 0) {
                                $("#ccd-list").append('<div class="weui-loadmore weui-loadmore_line"><span class="weui-loadmore__tips">暂无数据</span></div>');
                            }
                        } else {
                            $("#ccd-list").append('<div class="weui-loadmore weui-loadmore_line"><span class="weui-loadmore__tips">暂无数据</span></div>');
                        }
                        if (sessionStorage.getItem("list_scrollTop")) {
                            $(".ccdList-page").scrollTop(sessionStorage.getItem("list_scrollTop"));
                            sessionStorage.removeItem("list_scrollTop");
                        }
                    } catch (err) {
                        $.alert("数据错误:" + err);
                    }
                } else if (data["code"] == 501) {
                    $("#ccd-list").append('<div class="weui-loadmore weui-loadmore_line"><span class="weui-loadmore__tips">暂无数据</span></div>');
                } else {
                    $.alert(data["errmsg"]);
                }
                $.hideLoading();
            }, "json")
        }
    </script>
{% endblock %}